/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

/**
 *
 * @author sheked
 */
public class ClientsDB {
    private QueryRunner run;
    private MapHandler mh;

    public ClientsDB() throws NamingException, SQLException {
            Context ctx = new InitialContext();
            DataSource odsconn = (DataSource)ctx.lookup("jdbc/volansbd");
            run = new QueryRunner(odsconn);
            mh = new MapHandler();
    }

    public Map<String, Object> consultaClient(String user, String pass) throws SQLException {
        return run.query("select id_client,nom,cognoms,admin,numprodpag from clients where email=? and password=?", mh, user, pass);
    }
    
    public boolean introdueixClient(String nom, String cognoms, String pass, Date datanaix, String adreca,
                                      int cp, String poblacio, int pais, String telefon, String email, int banc,
                                      int ofi, int dc, long cc) throws SQLException {
        int num = run.update("INSERT INTO \"IG21254\".\"CLIENTS\" VALUES (idclient.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    cognoms, nom, adreca, cp, poblacio, telefon, email, pass, datanaix, banc, dc, cc, ofi, 0, pais, 9);
        if (num == 0) {
            return false;
        }
        return true;
    }

    public Map<String, Object> consultaClient(int user) throws SQLException {
        return run.query("select nom,cognoms,admin from clients where id_client=?", mh, user);
    }
}
